package com.ruoyi.common.core.utils;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class CommonUtils {

    /**
     * 数字不等于null 且 大于0
     *
     * @param digit 判断值
     * @return 返回值
     */
    public static boolean isDigitNotNullAndGtZERO(Long digit) {
        if (isNull(digit)) return false;
        return digit > 0;
    }

    public static void main(String[] args) {
        String getRatioByTotal = isGetRatioByTotal(50, 0);
        System.out.println(getRatioByTotal);
    }

    public static String isGetRatioByTotal(Integer total, Integer number) {
        if (total == 0) return "0%";
        BigDecimal bigDecimal = BigDecimal.valueOf(number).divide(BigDecimal.valueOf(total)).multiply(BigDecimal.valueOf(100)).setScale(0, BigDecimal.ROUND_DOWN);
        return bigDecimal.toString() + "%";
    }

    /**
     * 生成订单号
     *
     * @param payType 0微信,1支付宝
     * @param code    0微信,1安卓,2IOS,3H5
     * @param length  长度 最少32位
     * @return String
     */
    public static String getOrderNumber(Integer payType, Integer code, Integer length) {
        Assert.isTrue(length >= 32, "订单号长度最少为32位");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String format = formatter.format(LocalDateTime.now());
        String s = format + payType + code;
        int i = length - s.length();
        String s1 = RandomUtils.aAndZ0And9(i);
        return s + s1;
    }

    /**
     * @param pattern 前面的图案 such as "=============="
     * @param code    颜色代号：背景颜色代号(41-46)；前景色代号(31-36)
     * @param n       数字+m：1加粗；3斜体；4下划线
     * @param content 要打印的内容
     */
    public static void printSingleColor(String pattern, int code, int n, String content) {
        System.out.format("%s\33[%d;%dm%s%n", pattern, code, n, content);
        System.out.format("%s\33[%d;m%s%n", "", 41, "");
    }

    /**
     * 验证是不是数字包含小数
     */
    public static boolean isNumber(String value) {
        return value.matches("^[0.0-9.0]+$");
    }

    /**
     * 如果data 等于 空 则返回defaultData
     *
     * @param data        判断值
     * @param defaultData 默认值
     * @param <T>         返回值类型
     * @return 返回值
     */
    public static <T> T isEmptyDefault(T data, T defaultData) {
        if (data == null) return defaultData;
        if (data.getClass().equals(String.class)) {
            if (data == "") {
                return defaultData;
            }
        }
        return data;
    }

    /**
     * 十六进制转 ascii
     *
     * @param hex 十六进制
     */
    public static String hexToAscii(String hex) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < hex.length(); i += 2) {
            String substring = hex.substring(i, i + 2);
            stringBuilder.append((char) Integer.parseInt(substring, 16));
        }
        return stringBuilder.toString();
    }

    /**
     * 是否等于空
     */
    public static <T> boolean isNull(T data) {
        return data == null;
    }

    /**
     * 是否
     */
    public static <T> boolean isNumber(Integer data) {
        if (data == null) return true;
        return data == -1;
    }

    /**
     * 是否
     */
    public static <T> boolean isBigDecimal(BigDecimal data) {
        if (data == null) return true;
        return data.compareTo(BigDecimal.valueOf(-1)) == 0;
    }


    public static BigDecimal getDistanceM(BigDecimal distance) {
        if (distance.compareTo(BigDecimal.valueOf(1000)) < 0) {
            return distance;
        }
        return distance.divide(BigDecimal.valueOf(1000), 1, BigDecimal.ROUND_DOWN);
    }
}
